package m.stock.v1;

import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

/**
 * Created by maojianfeng on 8/5/16.
 */
public class RecordDistributor {
    private static Logger logger = Logger.getLogger(RecordDistributor.class);
    public static void distribute(List<StockRecord> records, String dir){
        if(records!=null){
            for(StockRecord record:records){
                try {
                    String stockCode = record.getStockCode();
                    String file2Write = dir+ File.separator+stockCode+".dat";
                    FileUtils.write(new File(file2Write),record2Line(record),"utf-8",true);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
            }
        }
    }
    private static String record2Line(StockRecord record){
        return new Gson().toJson(record)+"\n";
    }
}
